package com.example.myapplication.flow

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.orhanobut.logger.Logger
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

/**
 * 类描述:
 * 创建人:   tangchao$
 * 创建时间: 2021/9/7$ 8:35 AM$
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao$
 * 修改时间: 2021/9/7$ 8:35 AM$
 * 修改备注:  [说明本次修改内容]
 */
class FlowViewModel2 : ViewModel() {
    lateinit var value1: String
    var mutableLiveData: MutableLiveData<Any> = MutableLiveData()

    fun t1() {
        viewModelScope.launch {
            var fl = flow {
                emit(1)
                emit(4)
                emit(5)

            }
            fl.filter {
                it >= 4
            }.collect {
                Logger.d("FlowViewModel2===t1: " + it);
                value1 = it.toString()
            }

        }

    }

    suspend fun operator2() {
        flowOf(1, 2, 3, 4).transform {
            emit(it + 2)
        }.filter {
            it > 3
        }.map {
            "a+$it"
        }.take(2).onEach {
//            Logger.d("FlowViewModel2===operator2:onEach " + it);

        }.collect()
//                .collect {
//                    Logger.d("FlowViewModel2===operator2: " + it);
//                }

        sinle()
    }

    suspend fun sinle() {
        val flow1 = flowOf(1, 2, 3, 4).collect()

//        Logger.d("FlowViewModel2===sinle: " + flow1.single());

    }
}